package com.huawei123.hasee.cniao5store.mvp.interactor;

import com.huawei123.hasee.cniao5store.api.AppIntroductionApi;
import com.huawei123.hasee.cniao5store.api.IGetDataDelegate;
import com.huawei123.hasee.cniao5store.base.BaseActivity;
import com.huawei123.hasee.cniao5store.bean.AppIntroductionBean;
import com.huawei123.hasee.cniao5store.utils.JsonParseUtils;
import com.zhxu.library.http.HttpManager;
import com.zhxu.library.listener.HttpOnNextListener;

import javax.inject.Inject;

/**
 * 项目名称：Cniao5Store
 * 类描述：网络请求App详情数据并转换为封装对象
 * 创建人：luohao
 * 创建时间：2018/2/11 11:00
 * 修改人：luohao
 * 修改时间：2018/2/11 11:00
 * 修改备注：
 */

public class AppIntroductionFragmentInteractor {
    private IGetDataDelegate<AppIntroductionBean> delegate;

    @Inject
    public AppIntroductionFragmentInteractor() {

    }

    public void loadAppIntroduction(BaseActivity activity, String packageName, IGetDataDelegate<AppIntroductionBean> delegate) {
        this.delegate = delegate;
        AppIntroductionApi appIntroductionApi = new AppIntroductionApi(httpListener, activity, packageName);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(appIntroductionApi);
    }

    HttpOnNextListener<AppIntroductionBean> httpListener = new HttpOnNextListener<AppIntroductionBean>() {
        @Override
        public void onNext(AppIntroductionBean appIntroductionBean) {
            delegate.getDataSuccess(appIntroductionBean);
        }

        @Override
        public void onCacheNext(String string) {
            super.onCacheNext(string);
            AppIntroductionBean appIntroductionBean = JsonParseUtils.parseAppIntroductionBean(string);
            delegate.getDataSuccess(appIntroductionBean);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            delegate.getDataError(e.getMessage());
        }
    };
}
